﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace LabyCLI.Commands
{
    class cmd_taskkill
    {
        Terminal frm = frmMain.Instance;

        public void Main(List<string> args)
        {
            if (args.Count() > 1)
            {
                if (args.Count() < 3)
                {
                    if (args[1].ToLower() == "/pid")
                    {
                        int pid;
                        if (int.TryParse(args[0].ToString(), out pid))
                        {
                            try
                            {
                                Process pName = Process.GetProcessById(pid);
                                pName.Kill();

                                Terminal.WriteLine("The process '" + args[0] + "' was killed." + Environment.NewLine + Environment.NewLine);
                            }
                            catch (ArgumentException)
                            {
                                Terminal.WriteLine("The process '" + args[0] + "' is not running." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                            }
                        }
                        else
                        {
                            Terminal.WriteLine("The ID '" + args[0] + "' is not a vaild PID." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                        }
                    }
                    else if (args[1].ToLower() == "/im")
                    {
                        try
                        {
                            Process[] pName = Process.GetProcessesByName(args[0]);

                            foreach (Process process in pName)
                            {
                                process.Kill();

                                Terminal.WriteLine("The process '" + process.ProcessName + "' was killed." + Environment.NewLine + Environment.NewLine);
                            }
                        }
                        catch (ArgumentException)
                        {
                            Terminal.WriteLine("The process '" + args[0] + "' is not running." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                        }
                    }
                    else
                    {
                        Terminal.WriteLine("The second argument sets whether LabyCLI should search for a process id /PID or try to find the image name of the process /IM" + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                    }
                }
                else
                {
                    Terminal.WriteLine("Maximmum amount of parseable argument exceeded." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                }
            }
            else
            {
                int pid;
                if (int.TryParse(args[0].ToString(), out pid))
                {
                    try
                    {
                        Process pName = Process.GetProcessById(pid);
                        pName.Kill();

                        Terminal.WriteLine("The process '" + args[0] + "' was killed." + Environment.NewLine + Environment.NewLine);
                    }
                    catch (ArgumentException)
                    {
                        Terminal.WriteLine("The process '" + args[0] + "' is not running." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                    }
                }
                else
                {
                    Terminal.WriteLine("The ID '" + args[0] + "' is not a vaild PID." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
                }
            }
        }

        public void Main()
        {
            Terminal.WriteLine("Invaild amount of arguments." + Environment.NewLine, Properties.Settings.Default.Terminal_ErrorForeColor);
        }
    }
}
